package com.atguigu.exam.service.impl;

import com.atguigu.exam.config.properties.MinioProperties;
import com.atguigu.exam.service.FileUploadService;
import io.minio.*;
import io.minio.errors.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Service
@Slf4j
public class FileUploadServiceImpl implements FileUploadService {

    @Autowired
    private MinioClient minioClient;
    @Autowired
    private MinioProperties minioProperties;
    /**
     * 实现文件上传的核心业务方法
     * @Param folder 不同上传位置的文件夹
     * @Param file 上传的文件封装对象
     * @return 可以访问的文件地址
     */
    @Override
    public String uploadFile(String folder, MultipartFile file) throws Exception {
        //1.判断桶是否存在
        boolean bucketExists = minioClient.bucketExists(BucketExistsArgs.builder().bucket(minioProperties.getBucketName()).build());
        //2.不存在，创建桶，设置访问权限
        if (!bucketExists) {
            minioClient.makeBucket(MakeBucketArgs.builder().bucket(minioProperties.getBucketName()).build());
            String config = """
                                   {
                                         "Statement" : [ {
                                           "Action" : "s3:GetObject",
                                           "Effect" : "Allow",
                                           "Principal" : "*",
                                           "Resource" : "arn:aws:s3:::%s/*"
                                         } ],
                                         "Version" : "2012-10-17"
                                   }
                               """.formatted(minioProperties.getBucketName());
            minioClient.setBucketPolicy(SetBucketPolicyArgs.builder()
                    .bucket(minioProperties.getBucketName())
                    .config(config)
                    .build());
        }
        //3.处理上传的对象名
        String objectName = folder + "/" + new SimpleDateFormat("yyyyMMdd").format(new Date()) + "/" +
                UUID.randomUUID().toString().replaceAll("-","")+"_"+ file.getOriginalFilename();
        log.debug("文件上传核心业务方法，处理后的文件对象名：{}",objectName);
        //4.上传文件 putObject方法
        minioClient.putObject(PutObjectArgs.builder()
                .bucket(minioProperties.getBucketName())
                .contentType(file.getContentType())
                .object(objectName) //对象
                .stream(file.getInputStream(),file.getSize(),-1)
                .build());
        //5.拼接回显地址
        String url = String.join("/", minioProperties.getEndpoint(), minioProperties.getBucketName(), objectName);
        log.info("文件上传核心业务，完成{}文件上传，返回地址为：{}",objectName,url);
        return url;
    }
}
